package com.glamour.hlcity.common.util;

import org.apache.log4j.or.ObjectRenderer;

/**
 * 将TemplatedMessage类型的日志转换成字符串
 *
 * @author zhaoxing.liuzx
 */
public class TemplatedLogRenderer implements ObjectRenderer {
    private static final long serialVersionUID = 1817495813342514760L;

    public String doRender(Object o) {
        if (o instanceof TemplatedLog) {
            return generateLog((TemplatedLog) o);
        } else {
            return o.toString();
        }
    }

    /**
     * 通过模板将TemplatedLog转换成字符串
     * 
     * @param message
     * @return 
     */
    private String generateLog(TemplatedLog log) {
        String template = log.getTemplate();
        if (template != null) {
            return StringUtil.format(template, log.getParams());
        } else {
            return log.toString();
        }
    }
}
